package com.platform.modules.cpt.controller;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.platform.common.utils.RestResponse;
import com.platform.modules.cpt.dto.ProjectInofDto;
import com.platform.modules.cpt.entity.CptAcceptanceEntity;
import com.platform.modules.cpt.entity.CptAcceptanceReportEntity;
import com.platform.modules.cpt.entity.CptAcceptanceReportQualityEntity;
import com.platform.modules.cpt.entity.CptAcceptanceSuperviseTeamEntity;
import com.platform.modules.cpt.service.CptAcceptanceReportService;
import com.platform.modules.prj.entity.PrjProjectEntity;
import com.platform.modules.quality.vo.CommonResult;
import com.platform.modules.sys.controller.AbstractController;
import com.platform.modules.sys.entity.SysUserEntity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * Controller
 *
 * @author biaohuaming
 * @date 2020-02-24 15:28:20
 */
@Api(tags = "竣工验收报告")
@RestController
@RequestMapping("cpt/acceptance/report")
public class CptAcceptanceReportController extends AbstractController {

    @Autowired
    private CptAcceptanceReportService cptAcceptanceReportService;
    
    /**
     * @param prjId
     * @return
     */
    @ApiOperation(value = "查询竣工验收数据")
    @GetMapping("/view")
    public RestResponse view(@RequestParam String cptId) {
    	Map<String, Object> params = getParams();
    	params.put("cptId", cptId);
    	CptAcceptanceReportEntity entity = cptAcceptanceReportService.queryOne(params);
        return RestResponse.success().put("data", entity);
    }
    
    /**
     * @param cptAcceptanceReportEntity
     * @return
     */
    @ApiOperation(value = "保存竣工验收数据")
    @PostMapping("save")
    public RestResponse save(@RequestBody CptAcceptanceReportEntity cptAcceptanceReportEntity) {
    	SysUserEntity sysUserEntity = this.getUser();
    	
    	if(StringUtils.isEmpty(cptAcceptanceReportEntity.getId())) {
    		cptAcceptanceReportEntity.setCreateTime(new Date());
    		cptAcceptanceReportEntity.setCreateUser(sysUserEntity.getUserId());
    	}else {
    		cptAcceptanceReportEntity.setUpdateTime(new Date());
    		cptAcceptanceReportEntity.setUpdateUser(sysUserEntity.getUserId());
    	}
    	cptAcceptanceReportService.saveOrUpdate(cptAcceptanceReportEntity);
        return RestResponse.success().put("cptAcceptanceReport", cptAcceptanceReportEntity);
    }
    
    /**
     * 生成 工程质量监督报告(后续盖章)
     * @param cptAcceptanceEntity
     * @return
     */
    @ApiOperation(value = "生成-竣工项目审查(后续盖章)")
    @PostMapping("/jgxmscFile/create")
    public CommonResult createJgxmscFile(@RequestBody CptAcceptanceEntity cptAcceptanceEntity) {
    	Map<String, Object> result = cptAcceptanceReportService.createJgxmscFile(cptAcceptanceEntity, "竣工项目审查.pdf");
        return CommonResult.success(result);
    }
    
    @ApiOperation(value = "下载-竣工项目审查 文件流")
    @GetMapping("/jgxmscFile/download")
    public void downloadJgxmscFile(@RequestParam String cptId, HttpServletRequest request, HttpServletResponse response) {
    	cptAcceptanceReportService.downloadJgxmscFile(cptId, request, response, "CPT_ACCPTANCE_FILES_JGXMSC");
    }
    
    @ApiOperation(value = "下载-竣工项目审查 文件")
    @GetMapping("/jgxmscFile/sign/download")
    public void downloadSignJgxmscFile(@RequestParam String cptId, HttpServletRequest request, HttpServletResponse response) {
    	cptAcceptanceReportService.downloadSignJgxmscFile(cptId, request, response, "CPT_ACCPTANCE_FILES_JGXMSC");
    }
    

    @ApiOperation(value = "盖章-竣工项目审查 文件")
    @PostMapping("/jgxmscFile/sign")
    public RestResponse signJgxmscFile(HttpServletRequest request,MultipartFile file, 
    		@RequestParam String cptId, 
    		@RequestParam String attId,
    		@RequestParam(required=false) String fileName) {
    	
    	//获取已签章的合同
        String contentType = request.getContentType();
        
        if (contentType != null && contentType.toLowerCase().startsWith("multipart/")) {
        	cptAcceptanceReportService.signJgxmscFile(request, cptId, "CPT_ACCPTANCE_FILES_JGXMSC", "竣工项目审查.pdf");
        }else {
        	RestResponse.error();
        }
        return RestResponse.success();
    }

    
    /**
     * 生成 工程质量监督报告(后续盖章)
     * @param cptAcceptanceEntity
     * @return
     */
    @ApiOperation(value = "生成-单位工程质量评定(后续盖章)")
    @PostMapping("/dwgczlpdFile/create")
    public CommonResult createDwgczlpdFile(@RequestBody CptAcceptanceReportQualityEntity cptAcceptanceReportQualityEntity) {
    	Map<String, Object> result = cptAcceptanceReportService.createDwgczlpdFile(cptAcceptanceReportQualityEntity, "竣工项目审查.pdf");
        return CommonResult.success(result);
    }
    
    @ApiOperation(value = "下载-单位工程质量评定 文件流")
    @GetMapping("/dwgczlpdFile/download")
    public void downloadDwgczlpdFile(@RequestParam String id, HttpServletRequest request, HttpServletResponse response) {
    	cptAcceptanceReportService.downloadDwgczlpdFile(id, request, response, "CPT_ACCPTANCE_FILES_DWGCZLPD");
    }
    
    @ApiOperation(value = "下载-单位工程质量评定 文件")
    @GetMapping("/dwgczlpdFile/sign/download")
    public void downloadSignDwgczlpdFile(@RequestParam String id, HttpServletRequest request, HttpServletResponse response) {
    	cptAcceptanceReportService.downloadSignDwgczlpdFile(id, request, response, "CPT_ACCPTANCE_FILES_DWGCZLPD");
    }
    

    @ApiOperation(value = "盖章-单位工程质量评定 文件")
    @PostMapping("/dwgczlpdFile/sign")
    public RestResponse signDwgczlpdFile(HttpServletRequest request,MultipartFile file, 
    		@RequestParam String id, 
    		@RequestParam String attId,
    		@RequestParam(required=false) String fileName) {
    	
    	//获取已签章的合同
        String contentType = request.getContentType();
        
        if (contentType != null && contentType.toLowerCase().startsWith("multipart/")) {
        	cptAcceptanceReportService.signDwgczlpdFile(request, id, "CPT_ACCPTANCE_FILES_DWGCZLPD", "竣工项目审查.pdf");
        }else {
        	RestResponse.error();
        }
        return RestResponse.success();
    }
    
    
    
    @ApiOperation(value = "下载-工程质量监督报告 文件")
    @GetMapping("/gcjgysbgFile/download")
    public void downloadGcjgysbgFile(@RequestParam String id, @RequestParam String rebuildFlag, HttpServletRequest request, HttpServletResponse response) {
    	cptAcceptanceReportService.downloadGcjgysbgFile(id, request, response, "CPT_ACCPTANCE_FILES_GCJGYSBG", rebuildFlag);
    }

    /**
     * @param prjId
     * @return
     */
    @ApiOperation(value = "查询项目信息")
    @GetMapping("/prjInfo")
    public RestResponse prjInfo(@RequestParam String cptId) {
    	ProjectInofDto entity = cptAcceptanceReportService.prjInfo(cptId);
        return RestResponse.success().put("data", entity);
    }
    
}
